<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="600px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="分类名称" prop="categoryName">
        <el-input v-model="formData.categoryName" placeholder="请输入分类名称" />
      </el-form-item>
      
      <el-form-item label="分类编码" prop="categoryCode">
        <el-input v-model="formData.categoryCode" placeholder="请输入分类编码" />
      </el-form-item>
      
      <el-form-item label="父级分类" prop="parentId">
        <el-tree-select
          v-model="formData.parentId"
          :data="categoryTree"
          placeholder="请选择父级分类"
          node-key="id"
          :props="{ label: 'categoryName', value: 'id' }"
          check-strictly
        />
      </el-form-item>
      
      <el-form-item label="适用职业" prop="professionId">
        <el-select v-model="formData.professionId" placeholder="请选择适用职业" clearable>
          <el-option label="全部职业" value="" />
          <el-option label="电工" value="1" />
          <el-option label="焊工" value="2" />
          <el-option label="车工" value="3" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="技能等级" prop="skillLevel">
        <el-select v-model="formData.skillLevel" placeholder="请选择技能等级" clearable>
          <el-option label="全部等级" value="" />
          <el-option label="初级" value="1" />
          <el-option label="中级" value="2" />
          <el-option label="高级" value="3" />
          <el-option label="技师" value="4" />
          <el-option label="高级技师" value="5" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="formData.sort" :min="0" placeholder="请输入排序" />
      </el-form-item>
      
      <el-form-item label="分类描述" prop="categoryDesc">
        <el-input
          v-model="formData.categoryDesc"
          type="textarea"
          :rows="3"
          placeholder="请输入分类描述"
        />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :value="0">禁用</el-radio>
          <el-radio :value="1">启用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="submitForm" type="primary" :loading="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

const emit = defineEmits(['success'])
const dialogVisible = ref(false)
const dialogTitle = computed(() => (formData.value.id ? '修改题目分类' : '添加题目分类'))
const formLoading = ref(false)
const formRef = ref()
const formData = ref({
  id: undefined,
  categoryName: '',
  categoryCode: '',
  parentId: '',
  professionId: '',
  skillLevel: '',
  sort: 0,
  categoryDesc: '',
  status: 1
})
const formRules = ref({
  categoryName: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
  categoryCode: [{ required: true, message: '分类编码不能为空', trigger: 'blur' }]
})

// 分类树形数据
const categoryTree = ref([
  {
    id: '1',
    categoryName: '基础理论',
    children: [
      { id: '11', categoryName: '电工基础' },
      { id: '12', categoryName: '安全规范' }
    ]
  },
  {
    id: '2',
    categoryName: '实操技能',
    children: [
      { id: '21', categoryName: '设备操作' },
      { id: '22', categoryName: '故障排除' }
    ]
  }
])

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  resetForm()
  if (id) {
    formLoading.value = true
    try {
      // TODO: 加载分类详情
      formData.value.id = id
    } finally {
      formLoading.value = false
    }
  }
}

/** 提交表单 */
const submitForm = async () => {
  const elForm = formRef.value
  if (!elForm) return
  const valid = await elForm.validate()
  if (!valid) return
  formLoading.value = true
  try {
    const data = formData.value
    if (data.id) {
      // TODO: 修改题目分类
    } else {
      // TODO: 新增题目分类
    }
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    categoryName: '',
    categoryCode: '',
    parentId: '',
    professionId: '',
    skillLevel: '',
    sort: 0,
    categoryDesc: '',
    status: 1
  }
  if (formRef.value) {
    formRef.value.clearValidate()
  }
}

defineExpose({ open })
</script> 